/*************************************************************************
 * AUTHOR		 : Chris Hayes
 * STUDENT ID	 : 656960
 * ASSIGNMENT #5 : Multi-Dimensional Array - Tic Tac Toe
 * CLASS		 : CS1B
 * SECTION		 : MW12:30
 * DUE DATE		 : June 15, 2011
 *************************************************************************/

#include "tictacheader.h"

/**************************************************************************
* FUNCTION PlayGame
*__________________________________________________________________________
* This function plays one complete game of TicTacToe.  It calls upon many
*  other functions to do things such as get clear the board before play,
*  post a score board, display the current state of the board, get and
*  check input, switch the current token, check to see who won, and output
*  the winner of the game.
*__________________________________________________________________________
* PRE-CONDITIONS
*  player1 : The name of player1 should be previously defined  - Value
*  player2 : The name of player2 should be previously defined  - Value
*  score1  : The score of player1 should be previously defined - Reference
*  score2  : The score of player2 should be previously defined - Reference
*  ties	   : The number of ties should be previously defined   - Reference
*
* POST-CONDITIONS
*  score1, score2, or ties get incremented at the end of the function
*  	depending on the results of the game
*
**************************************************************************/

void PlayGame(string player1, int& score1,
			  string player2, int& score2, int& ties)
{
	//VARIABLES-----------------------------------------------------------|
	char board[NUM_ROWS][NUM_COLS];		//TicTacToe board  		-OUT & CALC
	int roundCount;						//number of rounds played
										//						-CALC
	char token;							//Current player's token to be
										// played				-OUT & CALC
	char whoWon;						//player that won		-CALC
	//--------------------------------------------------------------------|

	//Clear the board before play
	InitBoard(board);

	//Token X always plays first
	token = 'X';

	for(int i = 1; i < 6; i++)
	{
		//Clear the screen - this was moved from DisplayBoard
		// 	to accommodate the score board
		system("clear");

		//Outputs the current wins of each player and total amount of ties
		ScoreBoard(player1, score1, player2, score2, ties);

		//Outputs the current state of the board
		DisplayBoard(board);

		//User picks where to play next
		GetAndCheckInp(board, token, player1, player2);

		//On round 5 do first check win
		if(i == 5)
		{
			//returns '0' if no one has won yet
			whoWon = CheckWin(board);
			roundCount = i + 1;
		}

		//Switch the current player
		token = SwitchToken(token);
	}

	while(whoWon == '0')
	{
		//Clear the screen - this was moved from DisplayBoard
		// 	to accommodate the score board
		system("clear");

		//Outputs the current wins of each player and total amount of ties
		ScoreBoard(player1, score1, player2, score2, ties);

		//Outputs the current state of the board
		DisplayBoard(board);

		//User picks where to play next
		GetAndCheckInp(board, token, player1, player2);

		//check to see if anyone has won
		whoWon = CheckWin(board);

		//On round 9, if no one has won, it is a tie.
		if(roundCount == 9 && whoWon == '0')
		{
			//If no one has won at this point, it is a tie
			whoWon = 'T';
		}

		//switch token and increment round count before looping
		token = SwitchToken(token);
		roundCount++;
	}

	//KEEPING SCORE
	(whoWon == 'X' ? score1++
				   : (whoWon == 'O' ? score2++
									: ties++));

	//Clear the screen
	system("clear");

	//OUTPUT - out puts the scoreboard, the state of the board, and a
	// message of who won.
	ScoreBoard(player1, score1, player2, score2, ties);
	DisplayBoard(board);
	OutputWinner(whoWon, player1, player2);
}
